package com.gw.quotes.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gw.quotes.R;
import com.gw.quotes.adapter.PriceListAdapter;
import com.gw.quotes.entity.business.Priceinfo;
import com.gw.quotes.entity.business.Pricelist;
import com.gw.quotes.net.BaseServiceInter;
import com.gw.quotes.net.HttpCallBack;
import com.gw.quotes.net.Request;
import com.gw.quotes.net.Response;
import com.gw.quotes.net.service.PriceinfoQueryServiceImpl;
import com.gw.quotes.net.service.PricelistQueryServiceImpl;
import com.yxw.view.YXWXListView;
import com.yxw.view.YXWXListView.IXListViewListener;

public class PriceListActivity extends BaseActivity implements
		IXListViewListener {

	private String goodsclassguid;
	private ArrayList<Pricelist> pricelist;
	private Handler mHandler;
	private int page_size = 10;
	private int page_number = 1;

	@ViewInject(id = R.id.back_img, click = "back")
	private ImageView back_img;
	@ViewInject(id = R.id.title)
	private TextView title_text;
	@ViewInject(id = R.id.pricelist_listview)
	private YXWXListView pri_listview;
	@ViewInject(id = R.id.no_result_layout)
	private RelativeLayout no_result_layout;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_price_list);
		initView();
		getIntentData();
	}

	@Override
	public void onResume() {
		super.onResume();
		if ((pricelist = (ArrayList<Pricelist>) aCache
				.getAsObject(goodsclassguid + "_pricelist")) == null) {
			// 启动加载数据
			pri_listview.startRefresh();
		} else {
			Date lastdate = aCache.getLastSaveTime(goodsclassguid
					+ "_pricelist");
			pri_listview.setRefreshTime(lastdate == null ? "尚未更新" : DateFormat
					.format("yyyy-MM-dd hh:mm:ss", lastdate).toString());
			setListView(pricelist);
		}
	}

	private void initView() {
		mHandler = new Handler();
		pri_listview.setPullLoadEnable(false);
		pri_listview.setXListViewListener(this);
	}

	private void setListView(ArrayList<Pricelist> list) {
		if (list.size() > 0) {
			no_result_layout.setVisibility(View.GONE);
			pri_listview.setVisibility(View.VISIBLE);
			pri_listview.setAdapter(new PriceListAdapter(
					PriceListActivity.this, R.layout.item_search, list));
			pri_listview.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					Log.i("xd", "price"
							+ pricelist.get((int) id).getPriceguid());
					getPriceInfo(pricelist.get((int) id).getPriceguid(),
							(int) id);
				}
			});
		} else {
			no_result_layout.setVisibility(View.VISIBLE);
			pri_listview.setVisibility(View.GONE);
		}
	}

	private void getPriceInfo(final String priceguid, final int id) {
		dialog.show();
		BaseServiceInter httpService = new PriceinfoQueryServiceImpl();
		Request request = new Request();
		request.setPriceguid(priceguid);
		Response response = httpService.handler(request,
				new HttpCallBack<Response<Priceinfo>>() {
					@Override
					public void onFinished(Response<Priceinfo> response) {
						dialog.dismiss();
						if (response.getResult() == 1) {
							Intent intent = new Intent(PriceListActivity.this,
									PriceInfoActivity.class);
							intent.putExtra("title", pricelist.get(id)
									.getGoodsname());
							intent.putExtra("priceinfo", response.getObj());
							startActivity(intent);
							// aCache.put(priceguid + "_priceinfo",
							// response.getObj());
						} else {
							Toast.makeText(PriceListActivity.this, "请求失败", 1)
									.show();
						}
					}
				});
	}

	private void getIntentData() {
		Intent intent = getIntent();
		goodsclassguid = intent.getStringExtra("goodsclassguid");
		setTitle(intent.getStringExtra("goodstitle"));
	}

	private void setTitle(String title) {
		title_text.setText(title);
	}

	private void getPriceList(final String goodsclassguid) {
		dialog.show();
		BaseServiceInter httpService = new PricelistQueryServiceImpl();
		Request request = new Request();
		request.setGoodsclassguid(goodsclassguid);
		Response response = httpService.handler(request,
				new HttpCallBack<Response<ArrayList<Pricelist>>>() {
					@Override
					public void onFinished(
							Response<ArrayList<Pricelist>> response) {
						dialog.dismiss();
						if (response.getResult() == 1) {
							pricelist = response.getObj();
							aCache.put(goodsclassguid + "_pricelist", pricelist);
							setListView(pricelist);
						} else {
							Toast.makeText(PriceListActivity.this, "请求失败", 1)
									.show();
						}
						onLoad();
					}
				});
	}

	public void back(View v) {
		finish();
	}

	private void onLoad() {
		pri_listview.stopRefresh();
		pri_listview.stopLoadMore();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		pri_listview.setRefreshTime(df.format(new Date()));
	}

	@Override
	public void onLoadMore() {
		mHandler.post(new Runnable() {
			@Override
			public void run() {
				// page_number++;
				// requestData(CommonConfig.PULL_LOAD_MORE);
			}
		});
	}

	@Override
	public void onRefresh() {
		getPriceList(goodsclassguid);
	}
}
